package com.lagou.rpc.consumer;

import com.lagou.curator.ServerRegistryCenter;
import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.consumer.proxy.RpcClientProxy;
import com.lagou.rpc.pojo.User;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ClientBootStrap
 * @Description:
 * @Author: qjc
 * @Date: 2021/9/21 11:39
 */
public class ClientBootStrap {

    private static List<String> childrenList = new ArrayList<>();

    public static void main(String[] args) throws InterruptedException {
        ServerRegistryCenter serverRegistryCenter = new ServerRegistryCenter();
        childrenList = serverRegistryCenter.getChildren(ServerRegistryCenter.RPC_ROOT);
        serverRegistryCenter.registryWatch(ServerRegistryCenter.RPC_ROOT, childrenList);
        while (true) {
            for (String childrenPath : childrenList) {
                String childrenNodeData = serverRegistryCenter.getChildrenNodeData(childrenPath);
                String[] split = childrenNodeData.split(ServerRegistryCenter.RPC_COLON);
                String ip = split[0];
                int port = Integer.parseInt(split[1]);

                IUserService userService = (IUserService) RpcClientProxy.createProxy(IUserService.class, ip, port);
                User user = userService.getById(1, false);
                System.out.println("调用 " + childrenNodeData + " 结果：" + user);
            }
            Thread.sleep(5000);
        }
    }
}
